package com.example.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.util.R;
import com.example.kafka.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/kafka")
@Tag(name = "Kafka", description = "Kafka相关接口")
public class SendController {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping(value = "/send")
    @Operation(summary = "发送消息")
    public R sendMessage() {
        // 发送消息到Kafka主题
        // kafkaTemplate.send("orderCreate", message);
        User user = new User();
        user.setUserName("小王");
        user.setAge(18);
        kafkaTemplate.send("user-topic", JSON.toJSONString(user));
        return R.ok("消息发送成功");
    }

}
